//
//  PPUserInfoViewController.m
//  pickpic
//
//  Created by Jinxiao on 10/3/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPUserInfoViewController.h"
#import "PPComplainViewController.h"
#import "PPAbstractPictureViewController.h"
#import "PPAvatarSettingPreviewViewController.h"

@interface PPUserInfoViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *avatarView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *signatureLabel;
@property (strong, nonatomic) IBOutlet UILabel *accountLabel;
@property (strong, nonatomic) IBOutlet UILabel *genderLabel;
@property (strong, nonatomic) IBOutlet UILabel *birthdayLabel;
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;
@property (weak, nonatomic) IBOutlet UIView *line1;
@property (weak, nonatomic) IBOutlet UIView *line2;
@property (weak, nonatomic) IBOutlet UIView *line3;
@property (weak, nonatomic) IBOutlet UIView *line4;
@property (weak, nonatomic) IBOutlet UIView *line5;
@property (weak, nonatomic) IBOutlet UIView *line6;
@property (weak, nonatomic) IBOutlet UIView *line7;
@property (weak, nonatomic) IBOutlet UIView *line8;
@property (weak, nonatomic) IBOutlet UIView *line9;
@property (readwrite, nonatomic, strong) PPAccountProfiles *profiles;
@end

@implementation PPUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [_line1 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line2 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line3 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line4 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line5 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line6 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line7 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line8 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line9 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];

    self.tableView.allowsSelection = NO;

    _profiles = [PPAccountProfiles existingProfilesForUserID:_userID];

    __weak typeof(self) weakSelf = self;
    [_avatarView setActionBlock:^(UIControl *control) {
        PPAvatarSettingPreviewViewController *pictureViewController = [[PPAvatarSettingPreviewViewController alloc] init];
        pictureViewController.editDiabled = YES;
        pictureViewController.avatar = weakSelf.profiles.avatarURLString;
        [weakSelf.navigationController pushViewController:pictureViewController animated:YES];
    } forEvent:UIControlEventTouchUpInside];

    [self fetchUserPublicInfoWithCallback:nil];

    if ([_profiles.userID isEqualToString:DGCurrentAccount.userID])
    {
        UIView *footer = [[UIView alloc] init];
        self.tableView.tableFooterView = footer;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self reloadDatas];
}

- (void)fetchUserPublicInfoWithCallback:(void (^)(void))callback {
    __weak typeof(self)weakSelf = self;
    [PPNetworkService fetchPublicInfoWithTargetUserID:_userID callback:^(id data, NSError *error) {
        __strong typeof(weakSelf)strongSelf = weakSelf;

        if(strongSelf == nil) {
            return;
        }

        PPAccountProfiles *profiles = [PPAccountProfiles existingProfilesForUserID:strongSelf.userID];
        [profiles mergeParameters:data];
        [profiles.managedObjectContext saveContext];

        [strongSelf reloadDatas];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)reloadDatas {

    if(_profiles.avatarURLString.length > 0) {
        [_profiles.avatarURLString applyToImageView:_avatarView];
    } else {
        _avatarView.image = [UIImage imageNamed:@"person_normal"];
    }

    _nameLabel.text = _profiles.name;
    _signatureLabel.text = _profiles.signature ? : @"";
    _accountLabel.text = _profiles.account;

    switch(_profiles.gender.integerValue) {
        case PPUserGenderUnknown: {
            _genderLabel.text = @"女";
            break;
        }

        case PPUserGenderFemale: {
            _genderLabel.text = @"女";
            break;
        }

        case PPUserGenderMale: {
            _genderLabel.text = @"男";
            break;
        }
    }

    _birthdayLabel.text = _profiles.birthday ? : @"";
    _locationLabel.text = _profiles.city ? [NSString stringWithFormat:@"%@ %@", _profiles.state, _profiles.city] : @"";
}

- (IBAction)complainButtonDidPress:(id)sender {
    PPComplainViewController *vc = [[UIStoryboard storyboardWithName:@"Setting" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PPComplainViewController"];
    vc.type = @4;
    vc.relationID = [NSNumber numberWithLongLong:[_profiles.userID longLongValue]];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - PPSettingBirthdayDatePickerDelegate
- (void)didUpdateBirthdaySuccessfull:(NSString *)newBirthday {
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    profiles.birthday = newBirthday;
    [profiles.managedObjectContext saveContext];

    _birthdayLabel.text = newBirthday;
}

#pragma mark - PPSettingLocationPickerDelegate
- (void)didUpdateLocationSuccessfull:(NSString *)newProvince newCity:(NSString *)newCity {
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    profiles.state = newProvince;
    profiles.city = newCity;

    [profiles.managedObjectContext saveContext];

    _locationLabel.text = [NSString stringWithFormat:@"%@ %@", newProvince, newCity];
}
@end
